package com.cloud.service.impl;

import com.cloud.entity.Goods;
import com.cloud.entity.GoodsType;
import com.cloud.entity.PageInfo;
import com.cloud.mapper.GoodsMapper;
import com.cloud.service.GoodsService;
import com.cloud.utils.GlobalConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author .
 * created in 2020/1/14 22:52
 */

@Service
public class GoodsServiceImpl implements GoodsService {

    private Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);

    private GoodsMapper goodsMapper;

    public GoodsServiceImpl(GoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }

    @Override
    public int addGoodsType(GoodsType type) {
        Date date = new Date();
        type.setId(GlobalConstant.getRandomUUID());
        type.setCtime(date);
        type.setUtime(date);
        int result = goodsMapper.addType(type);
        if (result > 0) {
            logger.info("商品种类【{}】添加成功", type.getName());
            return GlobalConstant.SUCCESS;
        }
        logger.info("商品种类【{}】添加失败", type.getName());
        return GlobalConstant.ERROR;
    }

    @Override
    public int delGoodsType(String id) {
        int result = goodsMapper.delTypeById(id);
        if (result > 0) {
            logger.info("商品种类删除成功");
            return GlobalConstant.SUCCESS;
        }
        logger.info("商品种类删除失败");
        return GlobalConstant.ERROR;
    }

    @Override
    public int updateGoodsType(GoodsType type) {
        type.setUtime(new Date());
        int result = goodsMapper.updateType(type);
        if (result > 0) {
            logger.info("商品种类信息更新成功");
            return GlobalConstant.SUCCESS;
        }
        logger.info("商品种类信息更新失败");
        return GlobalConstant.ERROR;
    }

    @Override
    public PageInfo<GoodsType> findAllByPage(PageInfo<GoodsType> info, String name) {
        if (StringUtils.isEmpty(name)) name = null;
        info.setCount(goodsMapper.getTypeCount(name));
        info.setData(goodsMapper.findAllByPage((info.getPage() - 1) * info.getLimit(), info.getLimit(), name));
        return info;
    }

    @Override
    public GoodsType findById(String id) {
        return goodsMapper.findById(id);
    }

    @Override
    public GoodsType findByName(String name) {
        return goodsMapper.findByName(name);
    }

    @Override
    public Map<String, Object> getGoodsTypeEcharts() {
        List<String> typeNameList = new ArrayList<>();
        Map<String, Object> resultMap = new HashMap<>();
        List<GoodsType> types = goodsMapper.findAll();
        Map<String, Object> goodsMap = null;
        List<Map<String, Object>> goodsList = new ArrayList<>();
        for (GoodsType type : types) {
            typeNameList.add(type.getName());
            List<Goods> goods = goodsMapper.findGoodsByPage(null, null, type.getId(), null);
            goodsMap = new HashMap<>();
            goodsMap.put("name", type.getName());
            goodsMap.put("value", goods.size());
            goodsList.add(goodsMap);
        }
        resultMap.put("legendData", typeNameList);
        resultMap.put("seriesData", goodsList);
        return resultMap;
    }

    @Override
    public List<GoodsType> findAllType() {
        return goodsMapper.findAll();
    }

    @Override
    public int addGoods(Goods goods) {
        Date date = new Date();
        goods.setCode(UUID.randomUUID().toString().replace("-", ""));
        goods.setCtime(date);
        goods.setUpdater(goods.getCreater());
        goods.setUtime(date);
        goods.setStatus(1);
        int result = goodsMapper.addGoods(goods);
        if (result > 0) {
            logger.info("商品信息添加成功!");
            return GlobalConstant.SUCCESS;
        }
        logger.info("商品信息添加失败!");
        return GlobalConstant.ERROR;
    }

    @Override
    public int delGoods(int id) {
        int result = goodsMapper.delGoodsById(id);
        if (result > 0) {
            logger.info("商品删除成功!");
            return GlobalConstant.SUCCESS;
        }
        logger.info("商品删除失败!");
        return GlobalConstant.ERROR;
    }

    @Override
    public int updateGoods(Goods goods) {
        goods.setUtime(new Date());
        int result = goodsMapper.updateGoods(goods);
        if (result > 0) {
            logger.info("商品信息更新成功!");
            return GlobalConstant.SUCCESS;
        }
        logger.info("商品信息更新失败!");
        return GlobalConstant.ERROR;
    }

    @Override
    public PageInfo<Goods> findGoodsByPage(PageInfo<Goods> info, String type, String name) {
        if (StringUtils.isEmpty(type)) type = null;
        else type = goodsMapper.findByName(type).getId();
        if (StringUtils.isEmpty(name)) name = null;
        info.setCount(goodsMapper.getGoodsCountByPage(type, name));
        if (info.getPage() != null)
            info.setData(goodsMapper.findGoodsByPage((info.getPage() - 1) * info.getLimit(), info.getLimit(),
                    type, name));
        return info;
    }

    @Override
    public Goods findGoodsById(int id) {
        return goodsMapper.findGoodsById(id);
    }

    @Override
    public Goods findGoodsByName(String name) {
        return goodsMapper.findGoodsByName(name);
    }

    @Override
    public List<String> findAllGoodsByName(String name) {
        if (StringUtils.isEmpty(name)) name = null;
        return goodsMapper.findAllGoodsByName(name);
    }
}
